package com.ydy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ydy.biz.UserBiz;
import com.ydy.mapper.User;
import com.ydy.service.UserService;
import com.ydy.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class HelloController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserBiz userBiz;
    @GetMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

    @GetMapping("/users")

    public List<User> hello() {
        return userBiz.findAll();
    }

    /**
     * connect
     *
     * select * from user where id = ?
     * User user = new User ();
     * user.setId()
     * user.setName{}
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public User getUser(Integer id){
        return userService.getById(id);
    }

    /**
     * select * from user where name = ?
     * @param name
     * @return
     */
    @GetMapping("/getByName")
    public User getUser(String name){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getName, name);
        List<User> list = userService.list(wrapper);
        if(list.isEmpty()){
            return null;
        }
//        userService.getOne(wrapper.last("limit 1"));
        return list.get(0);
    }
    @PostMapping("/insert")
    public void insert(@RequestBody User user){
        if(StringUtils.isEmpty(user.getName()) ){
            return;
        }
        userBiz.addUser(user);
    }


    @PutMapping("/update")
    public void updateUserByName(@RequestBody User user){
        userBiz.updateUserByName(user);
    }

    @DeleteMapping("/deleteById")
    public void deleteById(Integer id){
        userBiz.deleteById(id);
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUserVo")
    public UserVo getUserVo(Integer id){
        return userBiz.getUserVo(id);
    }
    @PostMapping("/addUser")
    public void addUser(@RequestBody UserVo userVo){
        userBiz.addUserInfo(userVo);
    }

    @GetMapping("/getUserVoWithRedis")
    public UserVo getUserVoWithRedis(Integer id){
        return userBiz.getUserVoWithRedis(id);
    }

}